package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;

/**
 * @author: ${唐杰}
 * @date: 2021/4/7
 */
public class TimeADView extends RelativeLayout implements View.OnClickListener {

    private FrescoImage mFrescoImage;
    private TextView mSkip;
    private int countdown=5;
    private boolean isShow=false;
    public TimeADView(@NonNull Context context) {
        this(context,null);
    }

    public TimeADView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }


    public TimeADView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view=View.inflate(context, R.layout.view_time_ad,this);
        mFrescoImage = view.findViewById(R.id.fi_image);
        mSkip = view.findViewById(R.id.tv_skip);

        mSkip.setOnClickListener(this);
        setEnabled(false);
        setVisibility(GONE);

    }
    private Handler mHandler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            --countdown;
            if(countdown<=0){
                mHandler.removeCallbacks(runnable);
                backClick();
            } else {
                mHandler.postDelayed(runnable,1000);
            }
            mSkip.setText("跳过："+String.valueOf(countdown));



        }
    };


    public void setData(String path, int time){
        setEnabled(true);
        setVisibility(VISIBLE);
        this.animate().alpha(1).start();
        isShow=true;
        mHandler.removeCallbacks(runnable);
        FrescoUtils.getInstance().setPrefixProgressLoad(getContext(),mFrescoImage,path);
        countdown=time>0?time:5;
        mSkip.setText("跳过："+String.valueOf(countdown));
        mHandler.postDelayed(runnable,1000);

    }

    private Runnable runnable=new Runnable() {
        @Override
        public void run() {

            Message message=mHandler.obtainMessage();
            mHandler.sendMessage(message);
        }
    };

    @Override
    public void onClick(View v) {
        backClick();
    }

    private OnBackClick onBackClick;

    public OnBackClick getOnBackClick() {
        return onBackClick;
    }

    public void setOnBackClick(OnBackClick onBackClick) {
        this.onBackClick = onBackClick;
    }

    public interface OnBackClick{
        void OnBackClick();
    }
    //回调给主页面
    private void backClick(){
        if(mHandler!=null){
            mHandler.removeCallbacks(runnable);
        }
        setEnabled(false);
        this.animate().alpha(0).setDuration(500).start();
        setVisibility(GONE);
        isShow=false;
        if(onBackClick!=null){
            onBackClick.OnBackClick();
        }
    }

    public boolean isShow() {
        return isShow;
    }

    public void setShow(boolean show) {
        isShow = show;
    }
}
